﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>在线考试</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <div class="weadmin-nav">
        <span class="layui-breadcrumb" style="visibility: visible;">
            <a href="#">首页</a><span lay-separator="">/</span>
            <a href="#">考试管理</a><span lay-separator="">/</span>
            <a><cite>在线考试</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:30px">&#x1002;</i>
        </a>
    </div>
    <div class="weadmin-body">
        <div class="layui-row">
            试卷查询：
            <div class="layui-inline">
                <input type="text" id="sEPName" placeholder="试卷名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn" id="sreach"><i class="layui-icon">&#xe615;</i></button>
        </div>
        <table id="datalist" lay-filter="datalist"></table>
    </div>
    <script src="../../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/html" id="tool">
        <a class="layui-btn layui-btn-xs" lay-event="beginexam">开始考试</a>
    </script>
    <script>
        layui.use(['table', 'jquery', 'layer'], function () {
            var table = layui.table,
                $ = layui.jquery,
                layer = layui.layer;
            $.support.cors = true;
            $.ajaxSetup({
                cache: false,
                headers: {
                    "userid": JSON.parse(window.localStorage.authInfo).UserId,
                    "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                },
            })
            var depdata = {};
            $.ajax({
                url: ApiUrl + "Dep/Sel",
                type: 'post',
                success: function (data) {
                    if (data.StatusCode == 200) {
                        //depdata = data.Data;
                        $.each(data.Data, function (index, item) {
                            depdata[item.DepID] = item.DepName;
                        })
                    }
                },
            });
            //获取部门名称
            function GetDepNames(depids) {
                var depNames = "";
                depids = depids.length > 0 ? depids.substr(1, depids.length - 2) : "";
                $.each(depids.split(','), function (index, item) {
                    depNames += depdata[item] + ",";
                })
                return depNames.substring(0, depNames.length > 0 ? depNames.length - 1 : 0);
            }
            $.ajax({
                url: ApiUrl + "Exam/GetNoComplatedExam",
                type: 'get',
                dataType: 'json',
                success: function (data) {
                    if (data.StatusCode == 200 && data.Count != 0) {
                        layer.open({
                            content: "您尚有未完成的考试！"
                            , closeBtn: 0
                            , btn: ['继续考试', '结束考试']
                            , yes: function (index, layero) {
                                OpenExam(data.Data[0].EPID, data.Data[0].EUID);
                                layer.close(index);
                            }
                            , btn2: function (index, layero) {
                                CloseExam(data.Data[0].EUID);
                                layer.close(index);
                                //return false 开启该代码可禁止点击该按钮关闭
                            }
                        });
                    }
                }
            });
            function showList() {
                var swhere = $("#sEPName").val().trim().length > 0 ? JSON.stringify({ EPName: $("#sEPName").val().trim(), EPIsRelease: 1 }) : JSON.stringify({ EPIsRelease: 1 });
                table.render({
                    id: "datalist",
                    elem: '#datalist',
                    url: ApiUrl + 'ExamPaperMain/Search', //数据接口,
                    where: { swhere: swhere },
                    cellMinWidth: 80,
                    cols: [
                        [
                            //{ type: 'checkbox' },
                            { field: 'EPID', title: '编号', sort: true },
                            { field: 'EPName', title: '名称' },
                            //{ field: 'EPTID', title: '类型'},
                            { field: 'EPTName', title: '类型' },
                            { field: 'EPFullMarks', title: '满分' },
                            {
                                field: 'DepIDs', title: '考试部门', templet: function (d) {
                                    return GetDepNames(d.DepIDs);
                                }
                            },
                            { field: 'EPExamDuration', title: '时长(分)' },
                            //{ field: 'EPDescribe', title: '描述' },
                            { field: 'EPRoom', title: '考场' },
                            //{ field: 'EPIsRelease', title: '在发布' },
                            {
                                field: 'EPIsRelease', title: '在发布', templet: function (d) {
                                    return d.EPIsRelease == "0" ? "否" : "是";
                                }
                            },
                            //{ field: 'EPStartTime', title: '开始时间' },
                            {
                                field: 'EPStartTime', title: '开始时间', templet: function (d) {
                                    return d.EPStartTime.substring(0, 10);
                                }
                            },
                            //{ field: 'EPEndTime', title: '结束时间' },
                            {
                                field: 'EPEndTime', title: '结束时间', templet: function (d) {
                                    return d.EPEndTime.substring(0, 10);
                                }
                            },
                            //{ field: 'EPExaminer', title: '监考人' },
                            //{ field: 'EPAddEmpID', title: '创建者' },
                            //{ field: 'EPAddEmpName', title: '创建者' },
                            //{ field: 'EPAddDateTime', title: '创建时间' },
                            { fixed: 'right', title: '操作', align: 'center', toolbar: '#tool', width: 100, }
                        ]
                    ],
                    parseData: function (res) { //res 即为原始返回的数据
                        console.log(res)
                        return {
                            "code": res.StatusCode, //解析接口状态
                            "msg": res.Info, //解析提示文本
                            "count": res.Count, //解析数据长度
                            "data": res.Data //解析数据列表
                        };
                    },
                    response: { statusCode: 200 },//规定成功的状态码，默认：0
                    event: true,
                    defaultToolbar: [],
                    page: true
                });
            }
            //打开考试对话框
            function OpenExam(EPID, EUID) {
                var beginexam =  parent.layer.open({
                    type: 2,
                    title: '在线考试（<span style="color: red;">考试期间请不要刷新或关闭浏览器，否则可能造成答题信息丢失</span>）',
                    area: ['600px', '400px'],
                    closeBtn: 0,
                    content: './views/exam/onlineexam_normal.html?EPID=' + EPID + "&EUID=" + EUID,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.full(beginexam);
            }
            //关闭未完成的考试
            function CloseExam(EUID) {
                $.ajax({
                    url: ApiUrl + "Exam/CloseExam",
                    type: 'get',
                    dataType: 'json',
                    data: { EUID: EUID },
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            layer.msg(data.Info);
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
            }
            //监听工具条
            table.on('tool(datalist)', function (obj) {
                var trdata = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                //var tr = obj.tr; //获得当前行 tr 的DOM对象
                if (layEvent === 'beginexam') {
                    layer.confirm("是否确定开始考试？", function (index) {
                        $.ajax({
                            url: ApiUrl + "Exam/CreateExamInfo",
                            type: 'get',
                            dataType: 'json',
                            data: { EPID: trdata.EPID },
                            success: function (data) {
                                console.log(data)
                                if (data.StatusCode == 200 && data.Count != 0) {
                                    OpenExam(trdata.EPID, data.Data);
                                    layer.close(index);
                                }
                                else if (data.StatusCode == 403) {
                                    top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                        top.location.href = "http://" + top.location.host + "/login.html";
                                    })
                                }
                                else {
                                    layer.msg(data.Info);
                                }
                            }
                        });
                    })
                }
            });

            showList();
            $("#sreach").click(function () {
                showList();
            })
        });
    </script>
</body>
</html>